package com.zbkj.crmeb.order.channel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.enums.ProductTypeEnum;
import com.zbkj.crmeb.enums.StockAreaTempEnum;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.crmeb.core.common.CommonResult;
import com.utils.RestTemplateUtil;
import com.utils.unibuy.client.OpenClient;
import com.utils.unibuy.common.RequestMethod;
import com.utils.unibuy.model.order.OpenApiOrderSaveModel;
import com.utils.unibuy.model.order.OrderSaveGoodsModel;
import com.utils.unibuy.request.order.OrderSaveRequest;
import com.utils.unibuy.response.order.OrderSaveResponse;
import com.utils.unibuy.vo.order.OrderBaseVO;
import com.crmeb.core.config.RestTemplateConfig;
import com.zbkj.crmeb.enums.BarCodeEnum;
import com.zbkj.crmeb.enums.PurchaseOrderChannelTypeEnum;
import com.zbkj.crmeb.order.AbstractStoreOrderService;
import com.zbkj.crmeb.order.StoreOrderFactory;
import com.zbkj.crmeb.store.dao.StoreOrderDao;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.StoreProductAttrValueService;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.store.vo.StoreOrderInfoVo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CrisStoreOrderServiceImpl extends AbstractStoreOrderService {
    @Autowired
    RestTemplateUtil restTemplateUtil;
    @Resource
    private StoreOrderDao storeOrderDao;
    @Autowired
    RestTemplateConfig restTemplateConfig;
    @Autowired
    StoreProductAttrValueService storeProductAttrValueService;
    @Autowired
    StoreProductService storeProductService;
    @Autowired
    StoreOrderFactory storeOrderFactory;
    @Autowired
    private IChannelMerchantService channelMerchantService;
    
    /**
     * 该供应商只虚拟下采购单
     */
    @Override
    public CommonResult<Object> addChannelOrder(StoreOrder storeOrder , List<StoreOrderInfoVo> orderInfoVoList) {
        try {
            orderInfoVoList.forEach(order ->{
                if (9 != storeOrder.getSource()) {
                    storeOrder.setPushOrderNo(order.getChildOrderNumber());
                }
                
                pushOrder(storeOrder, order);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success();
    }

    private void pushOrder(StoreOrder storeOrder, StoreOrderInfoVo orderInfo) {
    	String orderId = storeOrder.getSource() == 9 ? storeOrder.getOrderId().split("-")[0] : storeOrder.getOrderId();

    	log.info("--------------- cris订单推送成功: {} ---------------", orderId);
        //新增采购单
        storeOrderFactory.insertPurchaseOrder(storeOrder, true, PurchaseOrderChannelTypeEnum.CRIS.getCode());
        //新增采购商品
        storeOrderFactory.insertPurchaseGoods(orderInfo, storeOrder, true, "CRIS成功", null);
		StoreOrder storeOrderUpdate = new StoreOrder();
		storeOrderUpdate.setId(storeOrder.getId());
		storeOrderUpdate.setPushStatus(1);
		storeOrderUpdate.setPushMessage("CRIS成功");
        storeOrderDao.updateById(storeOrderUpdate);
        log.info("--------------- CRIS采购单创建成功 ---------------");

    }

    @Override
    public CommonResult<Object> cancelChannelOrder(StoreOrder storeOrder, List<StoreOrderInfoVo> orderInfoVoList) {
        return null;
    }

    @Override
    public CommonResult<Object> getOrderInfo(String channelOrderSn) {
        return null;
    }

}
